package com.gf.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.Activity;
import com.gf.base.type.TicketType;
import com.gf.user.dao.TicketDao;
import com.gf.user.mapper.TicketMapper;
import com.gf.user.model.Ticket;

@Service("ticketDao")
public class TicketDaoImpl extends BaseDaoImpl implements TicketDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * select * from ticket where uuid=?
	 */
	@Value("${ticketDao.queryByUuidSql}")
	private String queryByUuidSql;

	/**
	 * ticketDao.insertSql=insert into ticket
	 * (userId,uuid,createTime,activity,ticketType) values
	 * (?,?,current_timestamp,?,?)
	 */
	@Value("${ticketDao.insertSql}")
	private String insertSql;

	/**
	 * update ticket set activity=? where id=?
	 */
	@Value("${ticketDao.updateActivitySql}")
	private String updateActivitySql;

	/**
	 * select * from ticket where id=?
	 */
	@Value("${ticketDao.queryByIdSql}")
	private String queryByIdSql;

	/**
	 * 
	 */
	@Override
	public Ticket queryByUuid(String uuid) {
		List<Ticket> tickets = jdbcTemplate.query(queryByUuidSql,
				TicketMapper.getMapper(), uuid);

		if (tickets == null || tickets.size() <= 0) {
			return null;
		} else {
			return tickets.get(0);
		}
	}

	@Override
	public long insert(long userId, String uuid, TicketType ticketType) {

		return this.insertWithIdReturn(insertSql, userId, uuid,
				Activity.NORMAL.toString(), ticketType.toString());
	}

	@Override
	public void updateActivity(long id, Activity activity) {
		jdbcTemplate.update(updateActivitySql, activity.toString(), id);
	}

	@Override
	public Ticket queryById(long id) {
		List<Ticket> tickets = jdbcTemplate.query(queryByIdSql,
				TicketMapper.getMapper(), id);
		if (tickets == null || tickets.size() <= 0) {
			return null;
		} else {
			return tickets.get(0);
		}

	}
}
